package com.wxj.book.controller;

import com.wxj.book.common.Const;
import com.wxj.book.pojo.Book;
import com.wxj.book.pojo.Category;
import com.wxj.book.pojo.User;
import com.wxj.book.service.BookService;
import com.wxj.book.service.BorrowVoService;
import com.wxj.book.service.CategoryService;
import com.wxj.book.vo.BorrowVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class PageController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BookService bookService;
    @Autowired
    private BorrowVoService borrowVoService;

    @RequestMapping("page/index")
    public String indexPage(Model model, HttpServletRequest request) {
        // 查询所有的分类
        List<Category> categoryList = categoryService.getAll();
        model.addAttribute("categoryList", categoryList);
        // 从数据库随机抽选四本书
        List<Book> books = bookService.getRandom();
        model.addAttribute("books", books);
        // 查询我的未归还记录
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        if (user != null) {
            List<BorrowVo> borrowVos = borrowVoService.getByStuNum(user.getStuNum());
            model.addAttribute("borrowVos", borrowVos);
        }
        // 查找最新到馆的四本图书
        List<Book> newBook = bookService.getNewBook();
        model.addAttribute("newBook", newBook);
        // 为您推荐书籍---十本
        List<Book> recommend = bookService.getRecommend();
        model.addAttribute("recommend", recommend);
        return "index";
    }

    @RequestMapping("404")
    public String error(Model model) {
        // 查询所有的分类
        List<Category> categoryList = categoryService.getAll();
        model.addAttribute("categoryList", categoryList);

        return "404";
    }

    /**
     *
     */
}
